package com.xiahui.day11.Test4;

/**
 * 模拟实现选民投票过程：一群选民进行投票，每个选民只允许投一次票，
 * 并且当投票总数达到100时，就停止投票
 */
public class Voter {
    String voterName;
    static int ticketCount = 100; // 因为是多个选民共享一个投票总数 所以定义为static修饰

    public Voter(String voterName) {
        this.voterName = voterName;
    }

    public boolean voteFor() {
        if (ticketCount > 0) {
            ticketCount--;
            System.out.println(voterName + "投出了第" + (100 - ticketCount) + "张票，还剩余" + (ticketCount) + "张票");
            return true;
        } else {
            System.out.println("投票结束");
            return false;
        }
    }

    public static void main(String[] args) {
        Voter zs = new Voter("张三");
        zs.voteFor();

        Voter gk = new Voter("李四");
        gk.voteFor();

        for (int i = 1; i <= 120; i++) {
            Voter v1 = new Voter(i + "号选民");
            System.out.println(v1);
//            boolean  result = v1.voteFor();
//            if(result == false){
//                break;
//            }

            if (!v1.voteFor()) {
                break;
            }

        }

    }


}
